package net.openl10n.packaging.project;

import net.openl10n.api.document.DocumentRef;
import net.openl10n.api.project.Project;
import net.openl10n.opc4j.PackagePart;
import net.openl10n.opc4j.exceptions.InvalidFormatException;
import net.openl10n.packaging.AbstractJaxbContentPart;
import net.openl10n.packaging.OpenLocPartContainer;
import net.openl10n.packaging.OpenLocPartFactory;
import net.openl10n.packaging.OpenLocPartFactoryManager;
import net.openl10n.packaging.document.DocumentPart;

import com.google.common.collect.ImmutableList;

public class ProjectPart extends AbstractJaxbContentPart{
	
	public static final String CONTENT_TYPE = "application/openl10n.project.main+xml";
	public static final String TARGET_NAME = "/project.xml";
	public static final String RELATIONSHIP_TYPE = "http://schemas.openl10n.net/project/main";
	
	private Project project;
	
	static final OpenLocPartFactory<ProjectPart> FACTORY = 
		new OpenLocPartFactory<ProjectPart>() {
			@Override
			public ProjectPart create(OpenLocPartContainer container, PackagePart part, OpenLocPartFactoryManager partFactoryManager) {
				return new ProjectPart(container, part, partFactoryManager);
			}

			@Override
			public String getContentType() {
				return CONTENT_TYPE;
			}

			@Override
			public String getRelationshipType() {
				return RELATIONSHIP_TYPE;
			}

			@Override
			public Class<ProjectPart> getTargetClass(){
				return ProjectPart.class;
			}
			
			@Override
			protected String getPartNamePattern() {
				return TARGET_NAME;
			};
		};
	
	protected ProjectPart(OpenLocPartContainer container, PackagePart part, OpenLocPartFactoryManager partFactoryManager){
		super(container, part, partFactoryManager); 
	}
	
	public ImmutableList<DocumentPart> getDocumentParts(){
		return getPartsOfType(DocumentPart.class);
	}
	
	public DocumentPart addDocumentPart(){
		try{
			DocumentPart documentPart = addNewPart(DocumentPart.class);
			return documentPart;
		}
		catch(InvalidFormatException e){
			throw new RuntimeException(e);
		}
	}
	
	public DocumentPart addDocumentPart(String fullPath, String contentType){
		try{
			DocumentPart documentPart = addNewPart(DocumentPart.class);

			DocumentRef doc = new DocumentRef(fullPath, contentType, getIdOfPart(documentPart) );
			
			documentPart.setDocument(doc);
			getProject().getDocuments().add(doc);
			
			return documentPart;
		}
		catch(InvalidFormatException e){
			throw new RuntimeException(e);
		}
	}
	
	public DocumentPart addDocumentPart(String id, String name, String path, String contentType){
		try{
			DocumentPart documentPart = addNewPart(DocumentPart.class);

			DocumentRef doc = new DocumentRef(id, name, path, contentType, getIdOfPart(documentPart) );
			
			documentPart.setDocument(doc);
			getProject().getDocuments().add(doc);
			
			return documentPart;
		}
		catch(InvalidFormatException e){
			throw new RuntimeException(e);
		}
	}
	
	public Project getProject() {
		if(project == null){
			project = loadOrCreateJaxbObject(Project.class);
		}
		return project;
	}
	
	public void setProject(Project project){
		this.project = project;
	}

	@Override
	protected void onClose() {
		if(project != null){
			saveJaxbObject(project);
		}
		super.onClose();
	}

}
